package ucc;

import java.util.List;

import dao.Dao;
import exceptions.InvalidParameterException;
import exceptions.NullParameterException;

public abstract class Service<E> {
	protected Dao<E> dao;
	
	protected Service(Dao<E> dao) {
		this.dao = dao;
	}
	
	protected abstract void businessRules(E entity) throws NullParameterException, InvalidParameterException;
	
	public E add(E entity) throws NullParameterException, InvalidParameterException {
		businessRules(entity);
		return this.dao.add(entity);
	}
	
	public E find(int id) throws InvalidParameterException {
		return this.dao.find(id);
	}
	
	public E remove(int id) throws InvalidParameterException {
		return this.dao.remove(id);
	}
	
	public E update(E entity) throws NullParameterException, InvalidParameterException {
		businessRules(entity);
		return this.dao.update(entity);
	}
	
	public List<E> listAll() {
		return this.dao.listAll();
	}
	
	public void saveToDatabase() {
		this.dao.saveDocumentToXMLFile();
	}
}
